<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
<!--        <h2 class="fl tac" v-if="JSON.stringify(searchCourseList)">-->
<!--          <span class="c-333">{{searchCourseValue}}等相关课程</span>-->
<!--        </h2>-->
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="javascript:void(0);" @click="searchAll" :class="{active:oneIndex===-1}">全部</a>
                </li>
                <li v-for="(item,index) in subjectNestedList" :key="item.id" :class="{active:oneIndex===index}">
                  <a :title="item.title" href="#" @click="searchOne(item.id,index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"/>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(item,index) in subSubjectList" :key="item.id" :class="{active:twoIndex===index}">
                  <a :title="item.title" href="javascript:void(0);" @click="searchTwo(item.id,index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"/>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <!--课程排序 begin-->
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange':buyCountSort!==''}">
                <a title="销量" href="javascript:void(0);" @click="searchBuyCount()">销量
                  <span :class="{hide:buyCountSort===''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':gmtCreateSort!==''}">
                <a title="最新" href="javascript:void(0);" @click="searchGmtCreate()">最新
                  <span :class="{hide:gmtCreateSort===''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':priceSort!==''}">
                <a title="价格" href="javascript:void(0);" @click="searchPrice()">价格&nbsp;
                  <span :class="{hide:priceSort===''}">↓</span>
                </a>
              </li>
            </ol>
          </section>
          <!--课程排序 end-->
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="data.total==0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul id="bna" class="of">
              <li v-for="item in searchCourseList" :key="item.id" v-if="JSON.stringify(searchCourseList)!= '{}'">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title">
                    <div class="cc-mask">
                      <a :href="'/course/'+item.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/'+item.id" :title="item.title" class="course-title fsize15">{{item.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span v-if="Number(item.price) === 0" class="fr jgTag bg-green">
                        <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span v-else class="price fr mr15" style="color: #e12228;font: 14px 微软雅黑;font-weight: 700">￥{{item.price}}</span>
                    <span class="fl jgAttr c-ccc f-fA" style="padding: 0px 10px;">
                        <i class="c-999 f-fA">{{ item.viewCount }}人学习</i>
                        |
                        <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
              <li v-for="item in data.list" :key="item.id" v-if="JSON.stringify(searchCourseList)== '{}'&&JSON.stringify(data.list)!= '{}'">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title">
                    <div class="cc-mask">
                      <a :href="'/course/'+item.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/'+item.id" :title="item.title" class="course-title fsize15">{{item.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span v-if="Number(item.price) === 0" class="fr jgTag bg-green">
                        <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span v-else class="price fr mr15" style="color: #e12228;font: 14px 微软雅黑;font-weight: 700">￥{{item.price}}</span>
                    <span class="fl jgAttr c-ccc f-fA" style="padding: 0px 10px;">
                        <i class="c-999 f-fA">{{ item.viewCount }}人学习</i>
                        |
                        <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"/>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)">首</a>
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current-1)">&lt;</a>
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{current: data.current == page, undisable: data.current == page}"
              :title="'第'+page+'页'"
              href="#"
              @click.prevent="gotoPage(page)">{{ page }}</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="后一页"
              @click.prevent="data.hasNext?gotoPage(data.current+1):''">&gt;</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)">末</a>
            <div class="clear"/>
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
  import courseApi from "../../api/course";

  export default {
    data() {
      return {
        page: 1,//当前页
        data: {},//课程列表
        subjectNestedList: [], // 一级分类列表
        subSubjectList: [], // 二级分类列表
        searchObj: {}, // 查询表单对象
        oneIndex: -1,//一级分类选中效果
        twoIndex: -1,//二级分类选中效果
        buyCountSort: "",
        gmtCreateSort: "",
        priceSort: "",
        categoryno1: this.$route.query.categoryno1,//index页面发过来类别查询 一级分类
        categoryno2: this.$route.query.categoryno2,// 二级分类
        searchCourseValue: '',//模糊查询课程名称
        searchCourseList:{},//模糊查询出来的课程
      }
    },
    created() {
      this.searchCourseValue = this.$route.query.searchCourseValue;
      //根据是否有参数来进行初始化
      if(this.searchCourseValue!=null){
        this.initCourseName();
      }
      else{
        //因为同时初始化会发生异步，所以先初始化课程类别再进行查询
        this.initSubject();
      }
      // console.log(JSON.stringify(this.searchCourseList)== '{}')

    },
    methods: {

      //对课程名称进行模糊查询
      initCourseName(){
        courseApi.queryCourseByName(this.searchCourseValue)
        .then(resp=>{
            this.searchCourseList = resp.data.data.courseList;
        })
      },

      initCourse() {

        if (this.categoryno1 && !this.categoryno2) {
          this.searchOne(this.categoryno1);
        } else if (this.categoryno1 && this.categoryno2) {
          this.searchOne(this.categoryno1);
          this.searchTwo(this.categoryno2);
        } else {
          //课程第一次查询
          this.initCourseFirst();
        }
      },


      //1.查询第一页的数据
      initCourseFirst() {
        courseApi.getCourseList(1, 8, this.searchObj)
          .then(resp => {
            this.data = resp.data.data;
            //console.log(this.data.list)
          })
      },
      //2.查询所有分类
      async initSubject() {
        await courseApi.getAllSubjectList()
          .then(resp => {
            this.subjectNestedList = resp.data.data.list;
            this.initCourse();
          })

      },
      //3.分页切换的方法
      gotoPage(page) {
        courseApi.getCourseList(page, 8, this.searchObj)
          .then(resp => {
            this.data = resp.data.data;
          })
      },
      searchAll(){
        window.location.href="/course"
        // this.searchOne('')
      },
      //4.点击一级分类显示二级分类
      searchOne(subjectParentId, index) {
        //由于index页面的侧边栏发过来的科目分类，没有index所以先查询所有的一级分类对比传过来的一级分类，然后赋值给index
        if (typeof (index) == "undefined") {
          //拿着点击一级分类的id和所有一级分类id比较
          for (let i = 0; i < this.subjectNestedList.length; i++) {
            //获取每一个一级分类
            var oneSubject = this.subjectNestedList[i];
            //进行比较
            if (oneSubject.id == subjectParentId) {
              index = i;
            }
          }
        }
        console.log("index:"+index)

        //把传递index赋值给oneIndex，为了active样式生效
        this.oneIndex = index;
        this.twoIndex = -1;

        //解决点完一级分类的二级分类后，再点别的一级分类数据显示问题
        this.searchObj.subjectId = '';
        this.subSubjectList = [];

        //点全部时无active效果解决
        if (subjectParentId == '') {
          this.oneIndex = -1;
        }

        //把一级分类id赋值给searchObj中的一级分类id
        this.searchObj.subjectParentId = subjectParentId;
        //自动跳转到第一页
        this.gotoPage(1);

        //拿着点击一级分类的id和所有一级分类id比较
        //如果id相同，则从一级分类中取出二级分类赋值
        for (let i = 0; i < this.subjectNestedList.length; i++) {
          //获取每一个一级分类
          var oneSubject = this.subjectNestedList[i];
          //进行比较
          if (oneSubject.id == subjectParentId) {
            //把一级分类中的二级分类集合赋值
            this.subSubjectList = oneSubject.children;
          }
        }
      },
      //5.点击二级分类查询课程
      searchTwo(subjectId, index) {
        //由于index页面的侧边栏发过来的科目分类，没有index所以先查询所有的一级分类下面的二级分类对比传过来的二级分类，然后赋值给index
        //console.log("ddd"+this.subjectNestedList[0].children[0].id)
        if (typeof (index) == "undefined") {
          //拿着点击一级分类的id和所有一级分类id比较
          for (let i = 0; i < this.subjectNestedList.length; i++) {
            //获取每一个一级分类
            var oneSubject = this.subjectNestedList[i];
            for (let j = 0; j < oneSubject.children.length; j++) {
              //进行比较
              if (oneSubject.children[j].id == subjectId) {
                index = j;
              }
            }
          }
        }
        //console.log("index:"+index);
        //把传递的index赋值给twoIndex，方便显示active
        this.twoIndex = index;

        //把传过来的二级分类id赋值给searchObj
        this.searchObj.subjectId = subjectId;
        //自动跳转到第一页
        this.gotoPage(1);

      },
      //根据销量排序
      searchBuyCount() {
        this.buyCountSort = "1";
        this.gmtCreateSort = "";
        this.priceSort = "";
        this.searchObj.buyCountSort = this.buyCountSort;
        this.searchObj.gmtCreateSort = this.gmtCreateSort;
        this.searchObj.priceSort = this.priceSort;
        this.gotoPage(this.page)
      },
//更新时间查询
      searchGmtCreate() {
        debugger
        this.buyCountSort = "";
        this.gmtCreateSort = "1";
        this.priceSort = "";
        this.searchObj.buyCountSort = this.buyCountSort;
        this.searchObj.gmtCreateSort = this.gmtCreateSort;
        this.searchObj.priceSort = this.priceSort;
        this.gotoPage(this.page)
      },
      //价格查询
      searchPrice() {
        this.buyCountSort = "";
        this.gmtCreateSort = "";
        this.priceSort = "1";
        this.searchObj.buyCountSort = this.buyCountSort;
        this.searchObj.gmtCreateSort = this.gmtCreateSort;
        this.searchObj.priceSort = this.priceSort;
        this.gotoPage(this.page)
      },

    },
  }
</script>
<style scoped>
  .active {
    color: #fff;
    background: #333;
    border-radius: 6px;
  }

  .active a, .active a:hover {
    color: #fff;
  }

  .hide {
    display: none;
  }

  .show {
    display: block;
  }

</style>
